package top.haijunit.work.converter;

import cn.hutool.core.map.MapUtil;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValueMappingStrategy;
import org.springframework.util.CollectionUtils;
import top.haijunit.work.modules.access.domain.PermissionGroupInfo;
import top.haijunit.work.modules.access.domain.PermissionInfo;
import top.haijunit.work.modules.access.domain.RoleInfo;
import top.haijunit.work.modules.access.entity.PermissionEntity;
import top.haijunit.work.modules.access.entity.PermissionGroupEntity;
import top.haijunit.work.modules.access.entity.RoleEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类型转换
 *
 * @author zhanghaijun
 * @since 2024-08-21
 */
@Mapper(componentModel = "spring", nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface AccessConverter {

    // 权限组类型转换
    @Mapping(target = "groupCode", source = "uniqCode")
    @Mapping(target = "groupName", source = "groupName")
    @Mapping(target = "groupDesc", source = "remarks")
    PermissionGroupInfo convert(PermissionGroupEntity entity);

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "createBy", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    @Mapping(target = "updateBy", ignore = true)
    @Mapping(target = "deleted", ignore = true)
    @Mapping(target = "uniqCode", source = "groupCode")
    @Mapping(target = "remarks", source = "groupDesc")
    PermissionGroupEntity convert(PermissionGroupInfo source);

    default List<PermissionGroupInfo> convertPermissionGroup(List<PermissionGroupEntity> sourceList) {
        return (CollectionUtils.isEmpty(sourceList) ? new ArrayList<>() : sourceList.stream().map(this::convert).toList());
    }

    // 权限类型转换
    @Mapping(target = "id", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "createBy", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    @Mapping(target = "updateBy", ignore = true)
    @Mapping(target = "deleted", ignore = true)
    @Mapping(target = "uniqCode", source = "code")
    PermissionEntity convert(PermissionInfo permission);

    // 角色类型转换
    @Mapping(target = "permissionMap", ignore = true)
    @Mapping(source = "entity.uniqCode", target = "roleCode")
    RoleInfo convert(RoleEntity entity);

    default RoleInfo convert(RoleEntity entity, Map<Long, Integer> permissionMap) {
        RoleInfo roleInfo = convert(entity);
        roleInfo.setPermissionMap(MapUtil.defaultIfEmpty(permissionMap, new HashMap<>()));
        return roleInfo;
    }

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "createBy", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    @Mapping(target = "updateBy", ignore = true)
    @Mapping(target = "deleted", ignore = true)
    @Mapping(target = "uniqCode", source = "roleCode")
    RoleEntity convert(RoleInfo info);

    default List<RoleInfo> convertPost(List<RoleEntity> sourceList) {
        return (CollectionUtils.isEmpty(sourceList) ? new ArrayList<>() : sourceList.stream().map(this::convert).toList());
    }
}
